<?php

namespace App\Jobs;

use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdateCommentCircleJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $post_arr = [];
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($post_arr)
    {
        $this->post_arr = $post_arr;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->post_arr)){
            $atta = WxComment::whereNull('posts_circle_id')->orderBy('id', 'desc')->limit(100)->get();
        }else{
            $atta = WxComment::whereNull('posts_circle_id')->whereIn('posts_id', $this->post_arr)->orderBy('id', 'desc')->get();
        }
        if($atta->isEmpty()){
            return;
        }
        $atta->map(function ($v){
            $circle_id = WxPost::where('id', $v->posts_id)->value('circle_id');
            if($circle_id && $circle_id > 0){
                WxComment::where('id', $v->id)->update(['posts_circle_id' => $circle_id]);
            }else{
                WxComment::where('id', $v->id)->update(['posts_circle_id' => 0]);
            }
        });
    }
}
